<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Ideas extends CI_Controller {

	public function index() {
		redirect('browse/');
	}

	public function addNewIdea() {
		//add new idea to database
		$this -> load -> model('idea_model');
		$this -> load -> library('form_validation');
		
		
		//validation rules
		$this -> form_validation -> set_rules('overview', 'Idea Overview', 'required' );
		$this -> form_validation -> set_rules('title', 'Idea Title', 'trim|required|is_unique[idea.idea_title]');
		$this -> form_validation -> set_rules('problem', 'Problem', 'required');
		$this -> form_validation -> set_rules('videoLink', 'Video Link', 'callback_YouTubeURLCheck');
		$this -> form_validation -> set_rules('solution', 'Solution', 'required');
		$this -> form_validation ->set_rules('tags', 'Tags');
		
		if ($this -> form_validation -> run() == FALSE) {
			
			//redirect to form
			include("authenticate.php");
			$this -> load -> view('post_ideas');

		} else {
			//form validated
			$overview = $this -> input -> post('overview');
			$title = $this -> input -> post('title');
			$category = $this -> input -> post('category');
			$problem = $this -> input -> post('problem');
			$solution = $this -> input -> post('solution');
			$date = date("Y-m-d H:i:s");
			$videoLink = $this -> input -> post('videoLink');
			$user_username = $this -> input -> post('user_username');
			$user_id = $this -> input -> post('user_id');
			
			$videoLink = $this->parseYouTubeURL($videoLink);

			$tag = strtolower($this -> input -> post('tags'));

			$data = array(
				'idea_overview' => $overview, 
				'idea_title' => $title, 
				'idea_category' => $category,
				'idea_problem' => $problem, 
				'idea_solution' => $solution, 
				'idea_date' => $date,
				'idea_video_link' => $videoLink,
				'user_user_username' => $user_username, 
				'user_user_id' => $user_id, 
				);

			

			$this -> load -> model('idea_model');
			$insertedIdea = (array)$this -> idea_model -> addIdea($data);
			
			if (!$insertedIdea) {
				//no idea is inserted into database
				echo "error";
			} else {
				//idea is added to database
				//add idea tags
				$idea_id = $insertedIdea['idea_id'];
				if(strlen(trim($tag)) !== 0){
					//check whether tag contains white space
					$tags = explode(';', $tag);
					$this -> idea_model -> addIdeaTags($idea_id, $tags);
				}
				//track activity
				$this->load->model('feed_model');
				$this->feed_model->trackUserActivity($user_username, " posted a new <a href = \"".base_url("ideas/byId/$idea_id")."\">idea.</a>");
				$this->feed_model->trackIdeaActivity($idea_id, " was created.");
				redirect('ideas/byId/' . $insertedIdea['idea_id']);
			}

		}
	}

	function YouTubeURLCheck($url){
		//parse the url
		$matched = $this->parseYouTubeURL($url);
		
		//if matched
		if(strlen($matched) > 0){
			
			if(strlen($matched) != 11){
				
				//valid youtube link
				$this->form_validation->set_message('YouTubeURLCheck', 'Video Link is Invalid');
				
				return false;
				
			}else{
				
				return true;
			}
			
		}else{
			
			return true;
		
		}
	}

	function parseYouTubeURL($url){
		//search for matched regex
		$regMatch = preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
		
			if($regMatch >0){
				
				return $matches[0];
				
			}else{
				
				return '';
			
			}		
	}

	function byId($ideaId){
	//view idea by id	
			if(!isset($ideaId)){
				//if idea id is null
				redirect('browse/');
			}
			
			if(!isset($_SESSION)){
				session_start();
			}
			
			//check subscription status
			if(isset($_SESSION['username'])){
				
				$this->load->model('feed_model');
				
				$username = $_SESSION['username'];
				
				$data['subscriptionIsTrue'] = $this->feed_model->checkSubscriptionIdea($ideaId, $username);
			}
			
			$this->load->model('idea_model');
			$this->load->model('comment_model');
			
			$data['idea'] = $this->idea_model->getIdeaById($ideaId);

			//update viewcount
			$this->idea_model->updateViewCount($data['idea']);

			//get related ideas
			$data['related_ideas'] = (array)$this -> idea_model -> getRelatedIdeas($ideaId);
			
			//get comments of this idea
			$data['comments'] = (array)$this -> comment_model -> getAllCommentsOf($ideaId);
			
			$this->load->view('view_idea', $data);
	}
	
	
	function bump(){
		//AJAX
		//Handle bump request
		
		$this->load->model('idea_model');
		
		$ideaId = $this->input->post('idea_id');
		$username = $this->input->post('username');
		
		$success = $this->idea_model->addBump($ideaId, $username);
		
		if($success == 0){
			//you already like this idea
			echo "You already bumped this idea";
		}else if ($success == 1){
			//success
			//track activity
			$this->load->model('feed_model');
			$this->feed_model->trackUserActivity($username, " bumped an <a href = \"".base_url("ideas/byId/$ideaId")."\">idea.</a>");
			
			echo "Bumped !";
			
		}
		
	}
	
	function addComment(){
		//Add comment to the and idea
		$this->load->model('comment_model');
		$this->load->model('feed_model');
		
		$idea_id = $this->input->post('idea_id');
		$username = $this->input->post('username');
		$comments = $this->input->post('comments');
		$comment_date = date("Y-m-d H:i:s");
		
		$data = array(
			'idea_idea_id' => $idea_id,
			'user_user_username' =>$username,
			'comment_date' => $comment_date,
			'comment_content' => $comments
			);
		
		$this->comment_model->addComment($data);
		
		//track activity
		$this->feed_model->trackUserActivity($username, " commented on an <a href = \"".base_url("ideas/byId/$idea_id")."\">idea.</a>");
		$this->feed_model->trackIdeaActivity($idea_id, " receive a new comment.");
		
		redirect('ideas/byId/'.$idea_id);
	}
	
	function userIdeas($username = NULL){
		//get all ideas of username
		
		if($username===NULL ){
			//username is null	
			show_404();
		}
		
		$this->load->model('idea_model');
		
		$data['username']= $username;
		$data['userideas']=(array)$this->idea_model->getAllUserIdeas($username);
		
		$this->load->view('view_useridea', $data);
	}
	
	
	

}

/* End of file post_ideas.php */
/* Location: ./application/controllers/post_ideas.php */
?>